package com.zt.rongda.stock.controller;

import com.zt.servlet.*;
import com.zt.rongda.stock.model.StockAlert;
import com.zt.rongda.stock.service.StockAlertService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping(value = "/stockAlert")
public class StockAlertController {

    @Autowired
    private StockAlertService stockAlertService;

    @RequestMapping(value = "/query")
    @ResponseBody
    
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(stockAlertService.getTable(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        stockAlertService.add(getStockAlert(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        stockAlertService.update(getStockAlert(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        stockAlertService.delete(reqBean.getInfo("stockId"));
        return Response.responseSuccess();
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private StockAlert getStockAlert(ReqBean reqBean) {
        StockAlert stockAlert = new StockAlert();
        stockAlert.setStockId(reqBean.getInfo("stockId"));
        stockAlert.setStockName(reqBean.getInfo("stockName"));
        stockAlert.setTypeId(reqBean.getInfo("typeId"));
        stockAlert.setTypeName(reqBean.getInfo("typeName"));
        stockAlert.setBrand(reqBean.getInfo("brand"));
        stockAlert.setSpec(reqBean.getInfo("spec"));
        stockAlert.setUnit(reqBean.getInfo("unit"));
        stockAlert.setAmount(reqBean.getInfoInt("amount"));
        return stockAlert;
    }

}